<?php
/**
 * Created by PhpStorm.
 * User: 10467
 * Date: 2020/11/7
 * Time: 10:04
 */

namespace app\admin\controller\Video;


use app\admin\controller\BaseController;
use app\admin\model\Video\VideoUserCommentModel;
use app\admin\model\Video\VideoUserModel;
use app\Utils\ImageUtil;
use app\Utils\Util;
use think\facade\Db;
use think\Request;
class Short extends BaseController
{
    public function getVideoList(Request $request)
    {

        $param = $request->param();
        $where = [];
        $page = $param['page'];
        $limit = $param['limit'];
        $video_title = !empty($param['video_title']) ? $param['video_title'] : "";
        $is_show = !empty($param['is_show']) ? $param['is_show'] : "";
        $status = !empty($param['status']) ? $param['status'] : "";
        if (empty($page)) {
            $page = 1;
        }
        if (empty($limit)) {
            $limit = 10;
        }

        if (!empty($video_title)) {
            array_push($where, ['title', 'like', '%' . $video_title . '%']);
        }
        if (!empty($is_show)) {
            if ($is_show == 1) {
                array_push($where, ['is_show', '=', 1]);
            }
            if ($is_show == 2) {
                array_push($where, ['is_show', '=', 0]);
            }
        }
        if (!empty($status)) {
            if ($status == 1) {
                array_push($where, ['status', '=', 1]);
            }
            if ($status == 2) {
                array_push($where, ['status', '=', 0]);
            }
        }
        $list = VideoUserModel::getVideoListWhere($page, $limit, $where);
        $count = VideoUserModel::getVideoCountWhere($where);
        foreach ($list as &$item){
            $item['label'] = !empty($item['label']) ?$item['label'] : "";
            $item['img'] = ImageUtil::i()->replaceUrl($item['img']);
            $images = !empty($item['slider_img']) ? json_decode($item['slider_img'],true) : [];
            foreach ($images as &$image){
                $image = ImageUtil::i()->replaceUrl($image);
            }
            $item['slider_img'] = $images;
            //评论列表
            $item['comments'] = VideoUserCommentModel::with(['user'])->where('video_id',$item['id'])->where('is_del',0)->order('id desc')->select();

        }
        unset($item);

        return Util::returnPageJson(self::CODE_DATA, $count, $list, "");
    }



    /**
     * 更新
     * @param Request $request
     * @return array
     */
    public function editVideo(Request $request)
    {
        $param = $request->param();
        $id = $param['id'];
        $video_title = !empty($param['title']) ? $param['title'] : "";
        $label = !empty($param['label']) ? $param['label'] : null;
        $brief = !empty($param['brief']) ? $param['brief'] : "";
        $url= !empty($param['url']) ? $param['url'] : "";
        $play_default = !empty($param['play_default']) ? $param['play_default'] : 0;
        $details = !empty($param['details']) ? $param['details'] : "";
        $sort = !empty($param['sort']) ? $param['sort'] : 0;

        $is_show = !empty($param['is_show']) ? $param['is_show'] : "";
        $status= !empty($param['status']) ? $param['status'] : "";
        if (empty($id)) {
            return Util::returnJson(self::CODE_ERROR, null, "参数错误");
        }
        $save = [
            'title' => $video_title,
            'label' => json_encode($label),
            'brief' => $brief,
            'url' => $url,
            'play_default' => $play_default,
            'details' => $details,
            'sort' => $sort,
            'up_time' => date("Y-m-d H:i:s",time())
        ];

        //图片上传(单图)
        if (!empty($request->file('file'))) {
            $file = $request->file('file');
            $path = "/video/flv/img/";
            $url = ImageUtil::i()->upload($file, $path);
        }
        //多图
        if (!empty($request->file('files'))) {
            $files = $request->file('files');
            $path = "/video/flv/slider_img/";
            $urls = ImageUtil::i()->upload($files, $path);
        }

        if (!empty($url)) {
            $save['img'] = $url;
        }
        if (!empty($urls)) {
            $save['slider_img'] = json_encode($urls);
        }

        if(isset($param['is_show'])){
            if (!empty($is_show) && $is_show == "on") {
                $save['is_show'] = 1;
            }else{
                $save['is_show'] = 0;
            }
        }

        if(isset($param['status'])){
            if (!empty($status) && $status == "on") {
                $save['status'] = 1;
            }else{
                $save['status'] = 0;
            }
        }


        Db::startTrans();
        try{
            VideoUserModel::updateVideoWhere([['id', '=', $id]], $save);
            Db::commit();
            return Util::returnJson(self::CODE_SUCCESS, null, "修改成功");
        }catch (\Exception $e){
            Db::rollback();
            var_dump($e->getMessage());
            return Util::returnJson(self::CODE_ERROR, null, "修改失败1");
        }
    }

    public function delComments(Request $request){

        $comment_id = $request->param('id');
        if (empty( $comment_id)) return Util::returnJson(self::CODE_ERROR,null,"评论ID必传");
        $where=[
            'id' =>$comment_id,
        ];

        $res = VideoUserCommentModel::dels($where);
        if($res){
            return Util::returnJson(self::CODE_SUCCESS,null,"删除成功");
        }else{
            return Util::returnJson(self::CODE_ERROR,null,"删除失败");
        }
    }
}